package comm.parsers;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import comm.util.SoptHashMap;

public class NaverSearchResultSAXParserHandler extends DefaultHandler {
	  private StringBuffer title = new StringBuffer();
	  private StringBuffer link = new StringBuffer();
	  private StringBuffer originallink = new StringBuffer();
	  private StringBuffer description = new StringBuffer();
	  private StringBuffer pubDate = new StringBuffer();
	  private StringBuffer bloggername = new StringBuffer();
	  private StringBuffer bloggerlink = new StringBuffer();
	  private StringBuffer thumbnail = new StringBuffer();
	  private StringBuffer height = new StringBuffer();
	  private StringBuffer width = new StringBuffer();
	  
	  private boolean isTitle = false;
	  private boolean isLink = false;
	  private boolean isDescription = false;
	  private boolean isOriginallink = false;
	  private boolean isPubDate = false;
	  private boolean isBloggername = false;
	  private boolean isBloggerlink = false;
	  private boolean isThumbnail = false;
	  private boolean isHeight = false;
	  private boolean isWidth = false;
	  
	  
	  private boolean isTotalcount = false;
	  
	  private String currentBuffer = "";
	  private String totalcount = "";
	  
	  public int itemNum=1;
	  
	  public ArrayList itemList = new ArrayList();
	  
	  public ArrayList getItemList(){
		  return itemList;
	  }
	  
	
	  
	  public void startElement(String uri, String localName,
	                     String qName, Attributes atts)
	  {
	      if (qName.equals("title")) {
	    	  currentBuffer = "title";
	    	  isTitle=true;  title.setLength(0); 
	      }else if (qName.equals("link")) {
	    	  isLink=true;  link.setLength(0); 
	      }else if (qName.equals("description")) {
	    	  currentBuffer = "description";
	    	  isDescription=true;  description.setLength(0); 
	      }else if (qName.equals("originallink")) {
	    	  isOriginallink=true;  originallink.setLength(0); 
	      }else if (qName.equals("pubDate")) {
	    	  isPubDate=true;  pubDate.setLength(0); 
	      }else if (qName.equals("bloggername")) {
	    	  isBloggername=true;  bloggername.setLength(0); 
	      }else if (qName.equals("isBloggerlink")) {
	    	  isBloggerlink=true;  bloggerlink.setLength(0); 
	      }else if (qName.equals("thumbnail")) {
	    	  isThumbnail=true;  thumbnail.setLength(0); 
	      }else if (qName.equals("sizeheight")) {
	    	  isHeight=true;  height.setLength(0); 
	      }else if (qName.equals("sizewidth")) {
	    	  isWidth=true;  width.setLength(0); 
	      }else if( qName.equals("total")){
	    	  isTotalcount=true;
	      }
	  }	  
	  
	  public void endElement(String uri, String localName,
	                     String qName)
	 { 
		  currentBuffer = "";
		  
		  if (qName.equals("item")){
	          //저장!~
			  SoptHashMap tempHm = new SoptHashMap();
			  
			  tempHm.put("totalcount",totalcount);
			  tempHm.put("title", title.toString());
			  tempHm.put("link", link.toString());
			  tempHm.put("description", description.toString());
			  tempHm.put("originallink", originallink.toString());
			  tempHm.put("pubdate", pubDate.toString());
			  tempHm.put("bloggername", bloggername.toString());
			  tempHm.put("bloggerlink", bloggerlink.toString());
			  tempHm.put("thumbnail", thumbnail.toString());
			  tempHm.put("width", width.toString());
			  tempHm.put("height", height.toString());
			  itemList.add(tempHm);
			  
	          itemNum++;
	      }else if (isTitle) {
	    	  isTitle=false; 
	      }else if (isLink) {
	    	  isLink=false;
	      }else if (isDescription) {
	    	  isDescription=false;
	      }else if (isOriginallink) {
	    	  isOriginallink=false;
	      }else if (isPubDate) {
	    	  isPubDate=false;
	      }else if (isBloggername) {
	    	  isBloggername=false;
	      }else if (isBloggerlink) {
	    	  isBloggerlink=false;
	      }else if (isThumbnail) {
	    	  isThumbnail=false;
	      }else if (isHeight) {
	    	  isHeight=false;
	      }else if (isWidth) {
	    	  isWidth=false;
	      }else if(isTotalcount){  
	    	  isTotalcount=false;
	      }
	  }
	  public void characters(char[] chars, int start, int leng) 
	  { 
		  String temp = new String(chars, start, leng);
		 
	      if (isTitle) {
	    	  isTitle=false; title.append(chars,start,leng);
	      }else if (isLink) {
	    	  isLink=false;  link.append(chars,start,leng);
	      }else if (isDescription) {
	    	  isDescription=false;  description.append(chars,start,leng);
	      }else if (isOriginallink) {
	    	  isOriginallink=false;  originallink.append(chars,start,leng);
	      }else if (isPubDate) {
	    	  isPubDate=false;  pubDate.append(chars,start,leng);
	      }else if(isTotalcount){
	    	  isTotalcount=false; totalcount = temp;
	      }else if (isBloggername) {
	    	  isBloggername=false; bloggername.append(chars,start,leng);
	      }else if (isBloggerlink) {
	    	  isBloggerlink=false; bloggerlink.append(chars,start,leng);
	      }else if(isThumbnail){
	    	  isThumbnail=false; thumbnail.append(chars,start,leng);
	      }else if (isHeight) {
	    	  isHeight=false; height.append(chars,start,leng);
	      }else if (isWidth) {
	    	  isWidth=false; width.append(chars,start,leng);
	      }else{ //<b>태그 문제 해결 부분- currentBuffer를 통해 < 태그 이후에 글씨들을 받아서 append
	    	  
	    	  if(currentBuffer.equals("title"))
	    		 title.append(temp);
	    	  else if(currentBuffer.equals("description"))
	    		  description.append(temp);
	      }
	  }
	
}
